/* 
    https://showme2.myhope365.com/online_course
 */

/* 设置课程分类 */
setClassClassify($(".course-list"))
setClassClassify($(".course-list-scroll"))
function setClassClassify(box) {
    $.ajax({
        url: "https://showme2.myhope365.com/online_course/weChat/applet/subject/list",
        type: "post",
        contentType: "application/json; charset=UTF-8",
        data: JSON.stringify({
            "enable": "1"
        }),
        success: (res) => {
            if (res.code == 0) {
                // console.log(res);
                // console.log(res.rows);
                // console.log($(".course-list"));

                res.rows.forEach((item) => {
                    box.append(`
                        <div class="course-item">
                            <a href="">${item.title}</a>
                        </div>
                    `)

                })

            }

        },
        error: (res) => {
            console.log(res);
        }
    })

}




/* $.ajax({
    url: "https://showme2.myhope365.com/online_course/weChat/applet/course/list/type",
    type: "post",
    data: {
        type: "free",
        pageNum: 1,
        pageSize: 5,
    },
    success: (res) => {
        if (res.code == 0) {
            // console.log(res);
            // console.log(res.rows);
            res.rows.forEach((item, index) => {
                // console.log(item);

                const container = $("#free-class .class-list-box");
                container.append(`
                    <div class=" class-list-item">
                            <div class="img-container">
                                <img src="./image/产品图1-1831543725516390400.jpg" alt="">
                            </div>
                            <div class="class-title"> ${item.courseTitle}</div>
                            <div class="class-info"> 共 ${item.subSectionNum} 节课| ${item.learningNum}  人报名 </div>

                            <div class="class-price">
                                <span class="free">
                                    ${item.isFree == 1 ? '免费' : ''}
                                </span>
                                <span class="price"> 
                                    ${item.isFree == 1 ? '' : `￥${item.coursePrice}`}
                                </span>
                                <span class="old-price">
                                    ${item.isFree == 1 ? '' : `￥${item.coursePrice}`}
                                </span>
                                
                                    ${item.isDiscount == 1 && item.isFree == 0 ? '<span class="discount-info">限时钜惠</span>':''}
                                
                            </div>
                            
                                
                          
                        </div>
                    `)


            })

        }

    },

}) */


/* 免费课程 */
getClassList("free", 1, 5, $("#free-class .class-list-box"))
/* 精品课程 */
getClassList("boutique", 1, 5, $("#boutique-class .class-list-box"))
/* 优惠课程 */
getClassList("discount", 1, 5, $("#discount-class .class-list-box"))

/* 
    type 课程类型,free,discount,boutique
    box 课程列表容器 jq对象
    获取课程列表 
*/
function getClassList(type, pageNum, pageSize, box) {
    $.ajax({
        url: "https://showme2.myhope365.com/online_course/weChat/applet/course/list/type",
        type: "post",
        data: {
            type: type,
            pageNum: pageNum,
            pageSize: pageSize,
        },
        success: (res) => {
            if (res.code == 0) {
                
                res.rows.forEach((item) => {
                    let imgUrl=item.bannerFileUrl.split("/");
                    // if (type == 'free')
                    //     console.log(item);
                    const container = box;
                    // https://course.myhope365.com/api/weChat/applet/course/list/type
                    // ./image/产品图1-1831543725516390400.jpg
                    //https://oos-hazz.ctyunapi.cn/online-course/${imgUrl[imgUrl.length-1]
                    //url(https://course.myhope365.com/api/weChat/applet/course/list/type/${imgUrl[imgUrl.length-1]}
                    container.append(`
                        <a href="../course_detail.html?courseId=${item.courseId}">
                    <div class=" class-list-item">
                            <div class="img-container">
                                <img src=" https://oos-hazz.ctyunapi.cn/online-course/${imgUrl[imgUrl.length-1]}" alt="">
                            </div>
                            <div class="class-title"> ${item.courseTitle}</div>
                            <div class="class-info"> 共 ${item.subSectionNum} 节课| ${item.learningNum}  人报名 </div>

                            <div class="class-price">
                                <span class="free">
                                    ${item.isFree == 1 ? '免费' : ''}
                                </span>
                                <span class="price"> 
                                    ${item.isFree == 1 ? '' : `￥${item.discountPrice}`}
                                </span>
                                <span class="old-price">
                                    ${item.isFree == 1 ? '' : `￥${item.coursePrice}`}
                                </span>
                                
                                    ${item.isDiscount == 1 && item.isFree == 0 ? '<span class="discount-info">限时钜惠</span>' : ''}
                                
                            </div>
                            
                                
                          
                        </div>
                        </a>
                    `)


                })

            }

        },
        error: (res) => {
            console.log(res);
        }

    })
}


/* 设置底部友情链接 */
$.ajax({
    url: "https://showme2.myhope365.com/online_course/system/dict/data/list/open",
    type: "post",
    data: {
        dictType: "blogroll",
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "dictSort",
        isAsc: "asc"
    },
    success: (res) => {
        // console.log(res);
        res.rows.forEach((item) => {
            $(".second-line").append(`
            
                <a href="${item.dictValue}">${item.dictLabel}</a>
            
            `)
        })
    },
    error: (res) => {
        console.log(res);
    }
})

/* 轮播图 */
$.ajax({
    url: "https://showme2.myhope365.com/online_course/weChat/applet/course/banner/list?number=5",
    type: "get",
    success: (res) => {
        if (res.code == 0) {
            /* 
                [index] 返回dom对象
                使用attr，需要将dom再封装成jq对象
                */
            res.data.forEach((item, index) => {
                const imgUrl = item.imgUrl.split("/");
                // console.log(imgUrl);
                $(".banner-img").eq(index).attr("src", `https://oos-hazz.ctyunapi.cn/online-course/${imgUrl[imgUrl.length - 1]}`)

            })

        }
    },
    error: (err) => {
        console.log(err);
    }

})

/* 轮播图 */
// $.ajax({
//     url:"https://showme2.myhope365.com/online_course/weChat/applet/course/banner/list",
//     type:"get",
//     success: function(res){
//         if(res.code === 0){
//             console.log(res);
//             res.data.forEach(item => {
//                 imgUrl = item.imgUrl.split("/");
//                 // console.log(imgUrl);
//                 $(".swiper-wrapper").append(`
//                     <div class="swiper-slide" style="background-image: url(https://oos-hazz.ctyunapi.cn/online-course/${imgUrl[imgUrl.length-1]});"></div>
//                 `)
//             });
//         }
//     }
// })

/* 按钮倒计时设置 */
function countDown(button, time) {
    button.attr("disabled", true);
    var timer = setInterval(() => {
        button.html(time + "s")
        time--;
        if (time == 0) {
            clearInterval(timer)
            button.attr("disabled", false);
            button.html("重新发送")
        }
    }, 1000)
}


// $("#login-checkCode-btn").bind({
//     click: () => {
//         $("#login-checkCode-btn").attr("disabled", true);
//         var time = 60;
//         var timer = setInterval(() => {
//             $("#login-checkCode-btn").html(time + "s")
//             time--;
//             if (time == 0) {
//                 clearInterval(timer)
//                 $("#login-checkCode-btn").attr("disabled", false);
//                 $("#login-checkCode-btn").html("重新发送")
//             }
//         }, 1000)

//     }
// })
// $("#register-checkCode-btn").bind({
//     click: () => {
//         $("#register-checkCode-btn").attr("disabled", true);
//         var time = 60;
//         var timer = setInterval(() => {
//             $("#register-checkCode-btn").html(time + "s")
//             time--;
//             if (time == 0) {
//                 clearInterval(timer)
//                 $("#register-checkCode-btn").attr("disabled", false);
//                 $("#register-checkCode-btn").html("重新发送")
//             }
//         }, 1000)

//     }
// })
// $("#forget-checkCode-btn").bind({
//     click: () => {
//         $("#forget-checkCode-btn").attr("disabled", true);
//         var time = 60;
//         var timer = setInterval(() => {
//             $("#forget-checkCode-btn").html(time + "s")
//             time--;
//             if (time == 0) {
//                 clearInterval(timer)
//                 $("#forget-checkCode-btn").attr("disabled", false);
//                 $("#forget-checkCode-btn").html("重新发送")
//             }
//         }, 1000)

//     }
// })
/* 隐藏模态框并移除遮罩 */
function hideModalWithBackdrop(modalId) {
    $(modalId).modal('hide'); // 隐藏模态框
    $('.modal-backdrop').remove(); // 移除遮罩层
    $('body').removeClass('modal-open'); // 移除 body 的 modal-open 类
}
/* 普通登录 */
$("#normal-login-btn").bind({
    click: () => {
        /*  */
        if ($("#username1").val() && $("#password1").val()) {
            $.ajax({
                url: "https://showme2.myhope365.com/online_course/pcUser/login",
                type: "post",
                data: {
                    username: $("#username1").val(),
                    password: $("#password1").val(),
                    rememberMe: true,
                },
                success: (res) => {
                    console.log(res);
                    if (res.code == 0) {
                        alert(`欢迎你，${res.userInfo.nickname}`)
                        hideModalWithBackdrop('#myModal');
                    } else if (res.code == 500) {
                        $(".pwd-tip").text("用户不存在/密码错误")
                    }
                }
            })
        }
    }
})

/*手机号登录 */
/* 验证手机号格式 */
function phoneRegexTest(phone) {
    const phoneRegex = /^1[3-9]\d{9}$/;
    return phone.length == 11 && phoneRegex.test(phone);

}
$("#phone-login-btn").bind({
    click: () => {
        // console.log($("#phone-login-mobile").val());
        // console.log($("#phone-check-code").val());
        const mobile = $("#phone-login-mobile").val().trim();
        const checkCode = $("#phone-check-code").val().trim();
        if (phoneRegexTest(mobile) && checkCode) {
            $.ajax({
                url: "https://showme2.myhope365.com/online_course/pcUser/login/mobile",
                type: "POST",
                data: {
                    mobile: $("#phone-login-mobile").val(),
                    code: $("#phone-check-code").val(),
                    rememberMe: false,
                },
                success: (res) => {
                    if (res.code == 0) {
                        alert(`欢迎你，${res.userInfo.nickname}`)
                        hideModalWithBackdrop('#myModal');
                    }
                    else {
                        $("#check-code-tip").html("验证码错误")
                    }
                },
                error: (err) => {
                    console.log(res);
                },

            })

        }

    }
})

/* 手机号登录 获取验证码 */
$("#login-checkCode-btn").bind({
    click: () => {
        const mobile = $("#phone-login-mobile").val().trim();

        const phoneRegex = /^1[3-9]\d{9}$/; // 11位，1开头，第二位3-9

        if (mobile && phoneRegex.test(mobile)) {
            // const mobile = 17733846495;
            $.ajax({
                url: `https://showme2.myhope365.com/online_course/pcUser/login/send/code/${mobile}`,
                type: 'get',
                success: function (res) {
                    console.log(res);

                    if (res.code == 0) {
                        console.log('发送成功');
                    } else {
                        console.log('发送失败');
                        $(".check-code-tip").html("验证码发送失败")
                    }
                }
            })
            countDown($("#login-checkCode-btn"), 60)
        }


    }
})

/* 普通注册 */
$("#register-btn").bind({
    click: () => {
        const gender = $('input[name="gender"]:checked').val();


        $.ajax({
            url: "https://showme2.myhope365.com/online_course/pcUser/register",
            type: "POST",
            data: {
                loginName: $("#reg-username").val(),
                code: $("#reg-check-code").val(),
                mobile: $("#reg-phone-number").val(),
                password: $("#reg-password").val(),
                nickname: $("#reg-nickname").val(),
                sex: `${gender == "male" ? 1 : 0}`,
                remark: '',
            },
            success: function (res) {
                if (res.code == 0) {
                    console.log(res);
                    alert(res.msg);
                } else if (res.code == 500) {
                    alert(res.msg);
                }

            },
            error: function (err) {

            }
        })

    }
})

/* 普通注册获取验证码 */
$("#register-checkCode-btn").bind({
    "click": function () {
        const mobile = $("#reg-phone-number").val();
        if (phoneRegexTest(mobile)) {
            $.ajax({
                url: `https://showme2.myhope365.com/online_course/pcUser/register/send/code/${mobile}`,
                type: 'get',
                success: function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        console.log('发送成功');
                    } else if (res.code == 500) {
                        console.log(res.msg);
                    }
                },
                error: function (res) {
                    console.log(res);
                }
            })
            countDown($("#register-checkCode-btn"), 60)
        } else {
            alert("请输入正确的手机号")
        }



    }
})
/* 忘记密码修改 */
$("#forget-ensure-btn").bind({
    click: () => {
        $.ajax({
            url: "",
            type: "post",
            data: {

            },
            success: (res) => {
                console.log(res);
                if (res.code == 0) {
                    console.log('normal');

                }
                else {
                    console.log('error');

                }
            },
            error: (res) => {

            }


        })

    }
})
/* 忘记密码 获取验证码 */
$("#forget-check-code-btn").bind({
    click: () => {
        const phone = $("#forget-phone-number").val();
        if (phoneRegexTest(phone)) {
            $.ajax({
                url: "https://showme2.myhope365.com/online_course/pcUser/forgetpassword/send/code",
                type: "post",
                data: {
                    loginName: forget - username,
                    mobile: forget - phone - number,
                    // loginName: 17733846495,
                    // mobile: 17733846495,
                },
                success: (res) => {
                    if (res.code == 0) {
                        console.log(res);
                    } else {
                        console.log(res);

                    }
                }

            })
            countDown($("#forget-checkCode-btn"), 60)
        } else {
            alert("请输入正确的手机号")
        }


    }
})

